const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchText: '', //搜索内容
    searchBaiKeHistory: [],
    isDelete: false,
    showNum: 10,
    showFlag: false,
    page: 1,
    size: 10,
    searchResult: [],
    isFinish: false
  },

  // 搜索
  searchHandle() {
    if (this.data.searchText == '') {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      })
      return
    }
    console.log(this.data.searchText);
    this.setData({
      page: 1
    })

    this.toSearch()
    var searchBaiKeHistory = this.data.searchBaiKeHistory,
      index = -1;
    index = searchBaiKeHistory.findIndex(x => x.text === (this.data.searchText))
    console.log(index);
    if (index != -1) {
      searchBaiKeHistory.splice(index, 1)
    }

    searchBaiKeHistory.unshift({
      text: this.data.searchText,
      checked: false
    })

    this.setData({
      searchBaiKeHistory
    })
    wx.setStorageSync('searchBaiKeHistory', JSON.stringify(searchBaiKeHistory))
  },

  toSearch() {
    var url = app.globalData.url + "ScBaiKe/listBaiKeSearch",
      that = this,
      searchResult = this.data.searchResult,
      isFinish = this.data.isFinish;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        searchName: that.data.searchText,
        page: that.data.page,
        size: that.data.size,
      }, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          searchResult = res.list
          isFinish = res.list.length <= that.data.size
          that.setData({
            searchResult,
            isFinish
          })
          console.log(isFinish);
          wx.hideLoading()
        }
      },
      err => {
        console.log(err);
      })
  },

  // 删除全部
  delHandle(e) {

    var flag = e.currentTarget.dataset.flag;

    if (flag == 'del') {
      // 显示删除按钮
      this.setData({
        isDelete: true
      })
    } else if (flag == 'delAll') {
      // 删除全部记录
      var thta = this;
      wx.showModal({
        title: '提示',
        content: '是否清空搜索记录',
        complete: (res) => {
          if (res.confirm) {
            wx.removeStorageSync('searchBaiKeHistory')
            thta.setData({
              searchBaiKeHistory: [],
              isDelete: false
            })
          }
        }
      })
    } else if (flag == 'cancle') {
      // 取消删除
      this.setData({
        isDelete: false
      })
    } else if (flag == 'item') {
      var index = e.currentTarget.dataset.index,
        searchBaiKeHistory = this.data.searchBaiKeHistory;
      if (this.data.isDelete) {
        searchBaiKeHistory.splice(index, 1)
        this.setData({
          searchBaiKeHistory
        })
        console.log(searchBaiKeHistory);
        wx.setStorageSync('searchBaiKeHistory', JSON.stringify(searchBaiKeHistory))
      } else {
        this.setData({
          searchText: searchBaiKeHistory[index].text,
          page: 1
        })
        this.toSearch()
      }
    } else if (flag == 'show') {
      var showNum = !this.data.showFlag ? this.data.searchBaiKeHistory.length : 10
      this.setData({
        showFlag: !this.data.showFlag,
        showNum
      })
    } else if (flag == 'clear') {
      console.log(111);
      this.setData({
        searchText: '',
        searchResult: []
      })
    }
  },

  toDetail(e) {
    var flag = e.currentTarget.dataset.flag,
      obj = e.currentTarget.dataset.obj;
    console.log(flag);
    console.log(obj);
    wx.navigateTo({
      url: '/contentPackage/pages/baikeDetail/baikeDetail?obj=' + JSON.stringify(obj),
    })


  },


  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
    this.searchHandle()
    console.log(this.data);
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var searchBaiKeHistory = wx.getStorageSync('searchBaiKeHistory') ? JSON.parse(wx.getStorageSync('searchBaiKeHistory')) : []
    this.setData({
      searchBaiKeHistory
    })
    console.log(searchBaiKeHistory);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {


  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isFinish) {
      wx.showToast({
        title: '暂无更多内容',
        icon: 'none'
      })
    } else {
      this.setData({
        page: this.data.page++
      })
      this.toSearch()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})